package tongcheng.com.tongcheng.ui.yemian.shenfen.activity.install;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.MyDailiShangBean;
import tongcheng.com.tongcheng.bean.PeoPleDetialBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GlideImageLoader;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.ShowToast;
import tongcheng.com.tongcheng.widght.MyBanner;

public class InstallOrderDetial extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.iorderdetial_banner)
    MyBanner mIorderdetialBanner;
    @Bind(R.id.iorderdetial_jieyue)
    Button mIorderdetialJieyue;
    @Bind(R.id.back_text)
    TextView mBackText;
    @Bind(R.id.title_line)
    View mTitleLine;
    @Bind(R.id.iorderdetial_username)
    TextView mIorderdetialUsername;
    @Bind(R.id.iorderdetial_address)
    TextView mIorderdetialAddress;
    @Bind(R.id.iorderdetial_phone)
    TextView mIorderdetialPhone;
    @Bind(R.id.iorderdetial_fuzeren)
    TextView mIorderdetialFuzeren;
    @Bind(R.id.scview)
    NestedScrollView scview;
    private int mId;
    private String mTemp;
    private int mIs_qIanyue;



    @Override
    public Integer initResid() {
        return R.layout.activity_install_order_detial;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {

        mId = getIntent().getIntExtra("id", -1);
        mTemp = getIntent().getStringExtra("temp");
        mIs_qIanyue = getIntent().getIntExtra("qianyue", -1);
        switch (mIs_qIanyue) {
            case 1://1签约等待确认
                mIorderdetialJieyue.setText("等待签约确认");
                break;
            case 2://2完成签约
                mIorderdetialJieyue.setText("解约");
                break;
            case 3://3未签约
                mIorderdetialJieyue.setText("签约");
                break;
            case 4://4解约待确认
                mIorderdetialJieyue.setText("等待解约确认");
                break;
            case -1://没状态.默认-1,签约
                mIorderdetialJieyue.setText("签约");
                break;

        }


        settitlebar(mBg);
        mTitle.setText("代理商详情");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        mIorderdetialJieyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String trim = mIorderdetialJieyue.getText().toString().trim();
                if (trim.equals("解约") || trim.equals("签约")) {
                    Intent intent = new Intent(InstallOrderDetial.this, JieYueActivity.class);
                    intent.putExtra("qianyue", mIs_qIanyue);
                    intent.putExtra("id", mId);
                    startActivity(intent);
                } else {
                    ShowToast.showToast("确认中,请耐心等待");
                }

                // startactivity(InstallOrderDetial.this, JieYueActivity.class);
            }
        });




        if (mTemp == null) {
            initData();
        } else {
            initMyDailiShang();
        }

    }

    private void initMyDailiShang() {

        MyRequest request = new MyRequest(ConStans.MyDailiShang_Url, RequestMethod.GET);
        request.add("userid", mId);
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("代理商详情......",json);
                MyDailiShangBean peoPleDetialBean = GsonUtil.GsonToBean(json, MyDailiShangBean.class);
                if (peoPleDetialBean != null) {
                    if (peoPleDetialBean.data != null & peoPleDetialBean.data.size() > 0) {
                        scview.setVisibility(View.VISIBLE);
                        MyDailiShangBean.DataBean dataBean = peoPleDetialBean.data.get(0);
                        bindData1(dataBean);
                    }

                }
            }
        });
    }

    private void initData() {
        MyRequest request = new MyRequest(ConStans.InfoDetial_Url);
        request.add("id", mId);
        CallServer.getInstance().addPost(this, request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("代理商详情..",json);
                PeoPleDetialBean peoPleDetialBean = GsonUtil.GsonToBean(json, PeoPleDetialBean.class);
                if (peoPleDetialBean != null) {
                    if (peoPleDetialBean.data != null) {
                        bindData(peoPleDetialBean.data);
                    }

                }
            }
        });
    }

    //绑定数据
    private void bindData(final PeoPleDetialBean.DataBean data) {
        List<Integer> objects = new ArrayList<>();
        objects.add(R.mipmap.moren);
        List<String> path = new ArrayList<>();
        if (!TextUtils.isEmpty(data.documents_features)) {
            String[] split = data.documents_features.split(",");
            for (int i = 0; i < split.length; i++) {
                path.add(ConStans.BaseUrl_Test_url + split[i]);
                //objects.add(R.mipmap.ic_launcher);
            }
        }

        //设置完成之前的图片
        //设置banner样式
        //mAllorderdetialBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //设置图片加载器
        mIorderdetialBanner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        if (path.size() == 0) {
            mIorderdetialBanner.setImages(objects);
        } else {
            mIorderdetialBanner.setImages(path);
        }

        Log.e("pathsize", path.size() + "");
        //mAllorderdetialBanner.setImages(path);
        //设置banner动画效果
        mIorderdetialBanner.setBannerAnimation(Transformer.DepthPage);
      /*  //设置标题集合（当banner样式有显示title时）
        mAllorderdetialBanner.setBannerTitles(titles);*/
        //设置自动轮播，默认为true
        mIorderdetialBanner.isAutoPlay(true);
        //设置轮播时间
        mIorderdetialBanner.setDelayTime(1500);
        //设置指示器位置（当banner模式中有指示器时）
        mIorderdetialBanner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        mIorderdetialBanner.start();

        //---------------------轮播图初始化完成------------------------
        mIorderdetialUsername.setText(data.user_name);//设置名字
        mIorderdetialAddress.setText(data.address_detail);//设置地址详情
        mIorderdetialPhone.setText(data.phone);//设置电话
        mIorderdetialFuzeren.setText(data.contact_name);//负责人

    }

    //绑定数据
    private void bindData1(final  MyDailiShangBean.DataBean data) {
        List<Integer> objects = new ArrayList<>();
        objects.add(R.mipmap.moren);
        List<String> path = new ArrayList<>();
        if (!TextUtils.isEmpty(data.documents_features)) {
            String[] split = data.documents_features.split(",");
            for (int i = 0; i < split.length; i++) {
                path.add(ConStans.BaseUrl_Test_url + split[i]);
                //objects.add(R.mipmap.ic_launcher);
            }
        }

        //设置完成之前的图片
        //设置banner样式
        //mAllorderdetialBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //设置图片加载器
        mIorderdetialBanner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        if (path.size() == 0) {
            mIorderdetialBanner.setImages(objects);
        } else {
            mIorderdetialBanner.setImages(path);
        }

        Log.e("pathsize", path.size() + "");
        //mAllorderdetialBanner.setImages(path);
        //设置banner动画效果
        mIorderdetialBanner.setBannerAnimation(Transformer.DepthPage);
      /*  //设置标题集合（当banner样式有显示title时）
        mAllorderdetialBanner.setBannerTitles(titles);*/
        //设置自动轮播，默认为true
        mIorderdetialBanner.isAutoPlay(true);
        //设置轮播时间
        mIorderdetialBanner.setDelayTime(1500);
        //设置指示器位置（当banner模式中有指示器时）
        mIorderdetialBanner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        mIorderdetialBanner.start();

        //---------------------轮播图初始化完成------------------------
        mIorderdetialUsername.setText(data.user_name);//设置名字
        mIorderdetialAddress.setText(data.address_detail);//设置地址详情
        mIorderdetialPhone.setText(data.phone);//设置电话
        mIorderdetialFuzeren.setText(data.contact_name);//负责人

    }


}
